﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using Apache.Cassandra;

namespace Cassandraemon
{
	public partial class CassandraConnectionConfigBuilder
		: DbConnectionStringBuilder
	{
		#region Constructors

		public CassandraConnectionConfigBuilder()
		{
		}

		public CassandraConnectionConfigBuilder(string connectionString)
		{
			if (!string.IsNullOrEmpty(connectionString))
			{
				this.ConnectionString = connectionString;
			}
		}

		#endregion

		#region CassandraConnectionConfig

		public CassandraConnectionConfig ToCassandraConnectionConfig()
		{
			return new CassandraConnectionConfig(this);
		}

		#endregion

		#region Type Conversions

		internal static string GetValueOrDefault(IDictionary dict, string name, string defaultValue)
		{
			if (dict.Contains(name))
			{
				return (string)dict[name];
			}
			return defaultValue;
		}

		internal static bool GetValueOrDefault(IDictionary dict, string name, bool defaultValue)
		{
			if (dict.Contains(name))
			{
				return bool.Parse((string)dict[name]);
			}
			return defaultValue;
		}

		internal static int GetValueOrDefault(IDictionary dict, string name, int defaultValue)
		{
			if (dict.Contains(name))
			{
				return int.Parse((string)dict[name]);
			}
			return defaultValue;
		}

		internal static TimeSpan GetValueOrDefault(IDictionary dict, string name, TimeSpan defaultValue)
		{
			if (dict.Contains(name))
			{
				return TimeSpan.Parse((string)dict[name]);
			}
			return defaultValue;
		}

		internal static string[] GetValueOrDefault(IDictionary dict, string name, string[] defaultValue)
		{
			if (dict.Contains(name))
			{
				return CassandraConnectionConfig.SplitHosts((string)dict[name]);
			}
			return defaultValue;
		}

		internal static T GetValueOrDefault<T>(IDictionary dict, string name, T defaultValue)
		{
			if (dict.Contains(name))
			{
				return (T)Enum.Parse(typeof(T), (string)dict[name]);
			}
			return defaultValue;
		}

		void SetValue(string name, string[] value)
		{
			var str = CassandraConnectionConfig.JoinHosts(value);
			base[name] = str;
		}

		void SetValue(string name, object value)
		{
			base[name] = value.ToString();
		}

		#endregion
	}
}
